package net.maku.events.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.events.vo.UserSnapshotAuditVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.events.convert.UserSnapshotConvert;
import net.maku.events.entity.UserSnapshotEntity;
import net.maku.events.service.UserSnapshotService;
import net.maku.events.query.UserSnapshotQuery;
import net.maku.events.vo.UserSnapshotVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 用户随手拍作品
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@RestController
@RequestMapping("events/user_snapshot")
@Tag(name = "用户随手拍作品")
@AllArgsConstructor
public class UserSnapshotController {
    private final UserSnapshotService userSnapshotService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('events:user_snapshot:page')")
    public Result<PageResult<UserSnapshotVO>> page(@ParameterObject @Valid UserSnapshotQuery query) {
        PageResult<UserSnapshotVO> page = userSnapshotService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('events:user_snapshot:info')")
    public Result<UserSnapshotVO> get(@PathVariable("id") Long id) {
        UserSnapshotEntity entity = userSnapshotService.getById(id);

        return Result.ok(UserSnapshotConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody UserSnapshotVO vo) {
        userSnapshotService.save(vo);
        return Result.ok();
    }

    @PostMapping("audit")
    @Operation(summary = "用户随手拍审核")
    @PreAuthorize("hasAuthority('events:user_snapshot:update')")
    public Result<String> audit(@RequestBody UserSnapshotAuditVO auditVO) {
        userSnapshotService.audit(auditVO);
        return Result.ok();
    }

    @GetMapping("whetherChoiceness/{id}/{status}")
    @Operation(summary = "更改是否精选")
    @PreAuthorize("hasAuthority('events:user_snapshot:whetherChoiceness')")
    public Result<String> whetherChoiceness(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        userSnapshotService.whetherChoiceness(id, status);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('events:user_snapshot:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        userSnapshotService.delete(idList);
        return Result.ok();
    }
}